﻿using System;
using System.Windows.Forms;
using DevExpress.XtraBars;
using DevExpress.XtraEditors;
using NNS.UI;

namespace QLTB.NSX.UI
{
    public partial class frmNSX : XtraForm
    {
        public frmNSX()
        {
            InitializeComponent();
            uc.SaveChanged += ucQuyen_SaveChanged;
            uc.SaveChanging += uc_SaveChanging;
            uc.SaveComplete += uc_SaveComplete;
        }

        private frmMain MyRibbon
        {
            get { return ParentForm as frmMain; }
        }

        private void uc_SaveComplete(object sender, SaveCompleteEventArgs e)
        {
            //throw new NotImplementedException();
            MyRibbon.ShowProcess = BarItemVisibility.Never;
        }

        private void uc_SaveChanging(object sender, SaveChangingEventArgs e)
        {
            //throw new NotImplementedException();
            MyRibbon.SetValue(e.Percent);
        }

        protected override void OnParentChanged(EventArgs e)
        {
            base.OnParentChanged(e);
            if (MyRibbon != null)
            {
                MyRibbon.MyRibbon.SetPopupContextMenu(this, MyRibbon.pm);
            }
        }

        private void RemoveEvent()
        {
            MyRibbon.iFilter.Checked = uc.isFilter;
            MyRibbon.iFilter.ItemClick -= iFilter_ItemClick;
            MyRibbon.iClose.ItemClick -= iClose_ItemClick;
            MyRibbon.iSave.ItemClick -= iSave_ItemClick;
            MyRibbon.iRefesh.ItemClick -= iRefesh_ItemClick;
            MyRibbon.iCopy.ItemClick -= iCopy_ItemClick;
            MyRibbon.iSelectAll.ItemClick -= iSelectAll_ItemClick;
            MyRibbon.iClear.ItemClick -= iClear_ItemClick;
            MyRibbon.iDelete.ItemClick -= iDelete_ItemClick;
        }

        private void AddEvent()
        {
            MyRibbon.iFilter.Checked = uc.isFilter;
            MyRibbon.iFilter.ItemClick += iFilter_ItemClick;
            MyRibbon.iClose.ItemClick += iClose_ItemClick;
            MyRibbon.iSave.ItemClick += iSave_ItemClick;
            MyRibbon.iCancel.ItemClick += iCancel_ItemClick;
            MyRibbon.iRefesh.ItemClick += iRefesh_ItemClick;
            MyRibbon.iCopy.ItemClick += iCopy_ItemClick;
            MyRibbon.iSelectAll.ItemClick += iSelectAll_ItemClick;
            MyRibbon.iClear.ItemClick += iClear_ItemClick;
            MyRibbon.iDelete.ItemClick += iDelete_ItemClick;
        }


        private void iDelete_ItemClick(object sender, ItemClickEventArgs e)
        {
            Delete();
        }

        private void iClear_ItemClick(object sender, ItemClickEventArgs e)
        {
            ClearContext();
        }

        private void iSelectAll_ItemClick(object sender, ItemClickEventArgs e)
        {
            SelectAll();
        }

        private void iCopy_ItemClick(object sender, ItemClickEventArgs e)
        {
            Copy();
        }

        private void iRefesh_ItemClick(object sender, ItemClickEventArgs e)
        {
            Nap();
        }

        private void iCancel_ItemClick(object sender, ItemClickEventArgs e)
        {
            Cancel();
        }

        private void iSave_ItemClick(object sender, ItemClickEventArgs e)
        {
            Save();
        }

        private void iClose_ItemClick(object sender, ItemClickEventArgs e)
        {
            Close();
        }


        private void iFilter_ItemClick(object sender, ItemClickEventArgs e)
        {
            if (!Visible) return;
            uc.isFilter = MyRibbon.iFilter.Checked;
            //throw new NotImplementedException();
        }

        private void ucQuyen_SaveChanged(object sender, SaveChangedEventArgs e)
        {
            //throw new NotImplementedException();
            if (e.NotSave)
            {
                MyRibbon.iSave.ImageIndex = 69;
                Text = "Quản lý Nhà sản xuất - Nhãn hiệu (*)";
                //this.Appearance.ForeColor = Color.Red;
            }
            else
            {
                MyRibbon.iSave.ImageIndex = 24;
                Text = "Quản lý nhà sản xuất - Nhãn hiệu";
            }
        }

        private void frmNhaCungCap_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (uc.NotSave)
            {
                DialogResult dialogResult = MyBox.ShowSave("Dữ liệu chưa được lưu!\nBạn có muốn lưu lại không?",
                                                           "Quản lý nhà cung cấp");

                if (dialogResult == DialogResult.Cancel)
                {
                    e.Cancel = true;
                    return;
                }
                if (dialogResult == DialogResult.Yes)
                {
                    uc.Save();
                }
            }

            Hide();
            e.Cancel = !Program._exit;
        }

        public void Nap()
        {
            uc.Nap();
        }

        public void Save()
        {
            uc.Save();
        }

        public void Delete()
        {
            uc.Deleted();
        }

        public void Cancel()
        {
            // uc.Nap();
            // uc.NotSave = false;
            uc.Cancel();
        }

        public void SelectAll()
        {
            uc.SelectAll();
        }

        public void ClearContext()
        {
            uc.ClearContext();
        }

        public void Copy()
        {
            uc.Copy();
        }

        private void frmNhaCungCap_Activated(object sender, EventArgs e)
        {
            if (MyRibbon != null)
            {
                MyRibbon.IgClose.Visible = true;
                MyRibbon.iEdit.Visible = true;
                MyRibbon.igPrint.Visible = false;
                MyRibbon.isPrint.Visibility = BarItemVisibility.Never;
                MyRibbon.iUndo.Visibility = BarItemVisibility.Always;
                MyRibbon.iCancel.Visibility = BarItemVisibility.Always;
                MyRibbon.isPrint.Visibility = BarItemVisibility.Never;
                MyRibbon.iSave.Visibility = BarItemVisibility.Always;
                MyRibbon.iClose.Visibility = BarItemVisibility.Always;
                MyRibbon.iRefesh.Visibility = BarItemVisibility.Always;
                // MyRibbon.iNavigate.Visible = false;
                // MyRibbon.IPrevious.Visibility=BarItemVisibility.Never;
                // MyRibbon.INext.Visibility = BarItemVisibility.Never;
                MyRibbon.iFilter.Visibility = BarItemVisibility.Always;

                MyRibbon.iCopy.Visibility = BarItemVisibility.Always;
                MyRibbon.iSelectAll.Visibility = BarItemVisibility.Always;
                MyRibbon.iClear.Visibility = BarItemVisibility.Always;
                MyRibbon.iDelete.Visibility = BarItemVisibility.Always;

                AddEvent();
            }

            Nap();
        }

        private void frmNhaCungCap_Deactivate(object sender, EventArgs e)
        {
            if (MyRibbon != null)
            {
                MyRibbon.IgClose.Visible = false;
                MyRibbon.iEdit.Visible = false;
                MyRibbon.igPrint.Visible = false;
                MyRibbon.isPrint.Visibility = BarItemVisibility.Never;
                MyRibbon.iUndo.Visibility = BarItemVisibility.Never;
                MyRibbon.iCancel.Visibility = BarItemVisibility.Never;
                MyRibbon.isPrint.Visibility = BarItemVisibility.Never;
                MyRibbon.iSave.Visibility = BarItemVisibility.Never;
                MyRibbon.iClose.Visibility = BarItemVisibility.Never;
                MyRibbon.iRefesh.Visibility = BarItemVisibility.Never;
                // MyRibbon.iNavigate.Visible = false;
                // MyRibbon.IPrevious.Visibility = BarItemVisibility.Never;
                // MyRibbon.INext.Visibility = BarItemVisibility.Never;
                MyRibbon.iFilter.Visibility = BarItemVisibility.Never;

                MyRibbon.iCopy.Visibility = BarItemVisibility.Never;
                MyRibbon.iSelectAll.Visibility = BarItemVisibility.Never;
                MyRibbon.iClear.Visibility = BarItemVisibility.Never;
                MyRibbon.iDelete.Visibility = BarItemVisibility.Never;

                RemoveEvent();
            }
        }

        protected override bool ProcessDialogKey(Keys keyData)
        {
            switch (keyData)
            {
                    //case Keys.Alt | Keys.D:
                    //    //MessageBox.Show("You are stupid");
                    //    return true;

                    //// some more cases...  

                    //case Keys.F1:
                    //    //ucToolBar_HelpClick(ucBar);
                    //    F11Execute();
                    //    return true;
                    //case Keys.F3:
                    //    F3Execute();
                    //    return true;
                    //case Keys.F4:
                    //    F4Execute();
                    //    return true;
                case Keys.F5:
                    Nap();
                    return true;
                    //case Keys.F6:
                    //    F9Execute();
                    //    return true;
                    //case Keys.F7:
                    //    F7Execute();
                    //    return true;
                    //case Keys.F8:
                    //    F8Execute();
                    //    return true;
                    //case Keys.F9:
                    //    F9Execute();
                    //    return true;
                    //case Keys.F11:
                    //    F11Execute();
                    //    return true;
                    //case Keys.F12:
                    //    F12Execute();
                    //    return true;
                case Keys.Control | Keys.S:
                    Save();
                    return true;
                case Keys.Control | Keys.W:
                    Save();
                    return true;
                case Keys.F2:
                    Save();
                    return true;
                case Keys.Alt | Keys.X:
                    Close();
                    return true;

                case Keys.F10:

                    Close();
                    return true;
            }

            return false;
        }
    }
}